//
//  QLMXmDouVC.m
//  Xiaomakuaixiu
//
//  Created by 龚碧容 on 2018/1/27.
//  Copyright © 2018年 hwd. All rights reserved.
//

#import "QLMXmDouVC.h"
#import "QLMBeeTabCell.h"

@interface QLMXmDouVC ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>{
    NSMutableArray *dataArray;
    NSMutableArray *dataZCArray;
    
    UITableView *_tetableview;
    UITableView *_tableview;
    
    QLMCustomTipView *tipView;
    QLMCustomTipView *tipView2;
    
    NSInteger srCurrentPage;
    NSInteger zcCurrentPage;
}

@end

@implementation QLMXmDouVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title=@"小马豆";
    self.navigationController.navigationBar.barTintColor=UIColorFromRGB(0xffc107);
    
    if (@available(iOS 11.0, *)) {
        _tetableview.contentInsetAdjustmentBehavior=UIScrollViewContentInsetAdjustmentNever;
        _tableview.contentInsetAdjustmentBehavior=UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
        self.automaticallyAdjustsScrollViewInsets=NO;
    }
    
    srCurrentPage=1;
    zcCurrentPage=1;
    
    dataArray=[NSMutableArray array];
    dataZCArray=[NSMutableArray array];
    
    [_beeNumBtn setTitle:_xiaomabeeStr forState:UIControlStateNormal];
    
    _beeScrollView.backgroundColor=[UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1];
    _beeScrollView.contentSize=CGSizeMake(2*kScreen_width,kScreen_height-205-64);
    [self changeButtonColor:_exportBtn andOtherBtn:_importBtn sr:YES];
    
    _srBanner.hidden=NO;
    _zcBanner.hidden=YES;
    
    _tetableview=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, kScreen_height-64-49-40-10) style:UITableViewStylePlain];
    _tetableview.backgroundColor=[UIColor clearColor];
    _tetableview.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    _tetableview.delegate=self;
    _tetableview.dataSource=self;
    _tetableview.tag=999;
    _tetableview.tableFooterView=[UIView new];
    [_beeScrollView addSubview:_tetableview];
    
    //上拉刷新
    _tetableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(reuqestSRBeeInfoData)];
    _tetableview.mj_footer.automaticallyHidden = YES;
    
    _tableview=[[UITableView alloc] initWithFrame:CGRectMake(kScreen_width, 0, kScreen_width, kScreen_height-64-49-40-10) style:UITableViewStylePlain];
    _tableview.backgroundColor=[UIColor clearColor];
    _tableview.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    _tableview.delegate=self;
    _tableview.dataSource=self;
    _tableview.tableFooterView=[UIView new];
    [_beeScrollView addSubview:_tableview];
    
    _tableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(reuqestZCBeeInfoData)];
    _tableview.mj_footer.automaticallyHidden = YES;
    
    tipView=[QLMCustomTipView sharedInstance];
    tipView.hidden = YES;
    [_tetableview addSubview:tipView];
    
    tipView2=[QLMCustomTipView sharedInstance];
    tipView2.hidden = YES;
    [_tableview addSubview:tipView2];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden=NO;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.barTintColor=kNavigationBarColor;
    
    [self _removeArray];
}

- (void)_removeArray{
    [dataArray removeAllObjects];
    srCurrentPage=1;
    
    [dataZCArray removeAllObjects];
    zcCurrentPage=1;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)reuqestSRBeeInfoData{
    NSString *token=[[NSUserDefaults standardUserDefaults] objectForKey:kLoginToken];
    NSDictionary *tokenDic=[NSDictionary dictionaryWithObjectsAndKeys:token,@"token", nil];
    
    NSMutableDictionary *paraDic=[NSMutableDictionary dictionary];
    [paraDic setObject:@(srCurrentPage) forKey:@"page"];
    [paraDic setObject:@"10" forKey:@"rows"];
    
    [self.view makeToastActivity:CSToastPositionCenter];
    [ClientTool postUrlPath:@"/rest/vipInfo/srPage" withParamers:paraDic andBody:tokenDic
                    success:^(id responseObject){
                        [self.view hideToastActivity];
                        [_tetableview.mj_footer endRefreshing];
                        NSInteger code = [[responseObject objectForKey:@"flag"] integerValue];
                        if (code == 1) {
                            
                            [dataArray addObjectsFromArray:responseObject[@"rows"]];
                            if (dataArray.count==0) {
                                tipView.hidden = NO;
                            }else{
                                if ([responseObject[@"rows"] count]<10) {
                                    ETToast(@"已全部加载完成");
                                }
                                srCurrentPage++;
                                tipView.hidden = YES;
                            }
                            [_tetableview reloadData];
                        }else{
                            ETToast(responseObject[@"msg"]);
                        }
                        
                    }failure:^(NSError *error){
                        [self.view hideToastActivity];
                        [_tetableview.mj_footer endRefreshing];
                        ETToast(@"服务器连接失败");
                    }
     ];
}

- (void)reuqestZCBeeInfoData{
    NSString *token=[[NSUserDefaults standardUserDefaults] objectForKey:kLoginToken];
    NSDictionary *tokenDic=[NSDictionary dictionaryWithObjectsAndKeys:token,@"token", nil];
    
    NSMutableDictionary *paraDic=[NSMutableDictionary dictionary];
    [paraDic setObject:@(zcCurrentPage) forKey:@"page"];
    [paraDic setObject:@"10" forKey:@"rows"];
    
    [ClientTool postUrlPath:@"/rest/vipInfo/zcPage" withParamers:paraDic andBody:tokenDic
                    success:^(id responseObject){
                        [_tableview.mj_footer endRefreshing];
                        NSInteger code = [[responseObject objectForKey:@"flag"] integerValue];
                        if (code == 1) {
                            
                            [dataZCArray addObjectsFromArray:responseObject[@"rows"]];
                            if (dataZCArray.count==0) {
                                tipView2.hidden = NO;
                            }else{
                                if ([responseObject[@"rows"] count]<10) {
                                    ETToast(@"已全部加载完成");
                                }
                                zcCurrentPage++;
                                tipView2.hidden = YES;
                            }
                            
                            [_tableview reloadData];
                        }else{
                            ETToast(responseObject[@"msg"]);
                        }
                    }failure:^(NSError *error){
                        [self.view hideToastActivity];
                        [_tableview.mj_footer endRefreshing];
                        ETToast(@"服务器连接失败");
                    }
     ];
}

#pragma mark -
- (IBAction)beeBtnAction:(id)sender {
    UIButton *btn=(UIButton *)sender;
    if (btn.tag==0) {
        [UIView animateWithDuration:0.5 animations:^{
            _beeScrollView.contentOffset=CGPointMake(0, 0);
            _srBanner.hidden=NO;
            _zcBanner.hidden=YES;
        }];
    }else{
        [UIView animateWithDuration:0.5 animations:^{
            _beeScrollView.contentOffset=CGPointMake(kScreen_width, 0);
            _srBanner.hidden=YES;
            _zcBanner.hidden=NO;
        }];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (![scrollView isKindOfClass:[UITableView class]]) {
        if (scrollView.contentOffset.x==kScreen_width) {
            [self changeButtonColor:_importBtn andOtherBtn:_exportBtn sr:NO];
            _srBanner.hidden=YES;
            _zcBanner.hidden=NO;
        }else if (scrollView.contentOffset.x==0){
            [self changeButtonColor:_exportBtn andOtherBtn:_importBtn sr:YES];
            _srBanner.hidden=NO;
            _zcBanner.hidden=YES;
        }
    }
}

- (void)changeButtonColor:(UIButton *)btn andOtherBtn:(UIButton *)otherBtn sr:(BOOL)srBool{
    [self _removeArray];
    if (srBool) {
        [self reuqestSRBeeInfoData];
    }else{
        [self reuqestZCBeeInfoData];
    }
    [btn setTitleColor:[UIColor colorWithRed:85/255.0 green:85/255.0 blue:85/255.0 alpha:1] forState:UIControlStateNormal];
    [otherBtn setTitleColor:[UIColor colorWithRed:255/255.0 green:174/255.0 blue:0/255.0 alpha:1] forState:UIControlStateNormal];
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
#pragma mark -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView.tag!=999) {
        return dataZCArray.count;
    }
    return dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QLMBeeTabCell *vipCell=[QLMBeeTabCell cellWithTableView:tableView];
    vipCell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    if (tableView.tag!=999) {
        if (dataZCArray.count!=0) {
            vipCell.nameLabel.text=dataZCArray[indexPath.row][@"typeName"];
            vipCell.descriptLabel.text=dataZCArray[indexPath.row][@"description"];
            vipCell.timeLabel.text=[self timeWithTimeIntervalString:dataZCArray[indexPath.row][@"data"]];
            vipCell.numLabel.text=[NSString stringWithFormat:@"%@",dataZCArray[indexPath.row][@"val"]];
            vipCell.numLabel.textColor=[UIColor colorWithRed:72/255.0 green:255/255.0 blue:0/255.0 alpha:1];
        }
    }else{
        if (dataArray.count!=0) {
            vipCell.nameLabel.text=dataArray[indexPath.row][@"typeName"];
            vipCell.descriptLabel.text=dataArray[indexPath.row][@"description"];
            vipCell.timeLabel.text=[self timeWithTimeIntervalString:dataArray[indexPath.row][@"data"]];
            vipCell.numLabel.text=[NSString stringWithFormat:@"+%@",dataArray[indexPath.row][@"val"]];
            vipCell.numLabel.textColor=[UIColor colorWithRed:255/255.0 green:0/255.0 blue:0/255.0 alpha:1];
        }
    }
    return vipCell;
}

- (NSString *)timeWithTimeIntervalString:(NSString *)timeString{
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]/ 1000.0];
    NSString* dateString = [formatter stringFromDate:date];
    return dateString;
}

@end
